﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Accountant_EnterCompanyDetail : BasePage
{
    private List<CompanyDetailEntity> list;
    private string str = "Detail";
    private List<DetailTypesEntity> DetailTypesList;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            list = new List<CompanyDetailEntity>();
            Session[str] = list;

            GridView1.DataSource = list;
            GridView1.DataBind();

            Bind();
        }
    }

    void Bind()
    {
        if (DetailTypesList == null)
        {
            DetailTypesList = new AuditorsBLL().GetDetailTypes();
        }

        dropCurrency.DataSource = DetailTypesList;
        dropCurrency.DataTextField = "TypeName";
        dropCurrency.DataValueField = "TypeId";
        dropCurrency.DataBind();

        GridView1.DataSource = list;
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string company = Request.QueryString["CompanyID"];
        list = Session[str] as List<CompanyDetailEntity>;
        if (list == null || list.Count == 0)
        {
            AlertMsg("添加信息不能为空");
            return;
        }

        string msg = "";
        //if (new AuditorsBLL().CreateCompanyDetail(list, out msg))
        //{

        //    string orderid = Request.QueryString["orderid"];
        //    Response.Redirect("CompanyDetail.aspx?CompanyID=" + company + "&orderid=" + orderid);
        //}
        //else
        //{
        //    if (msg.ToLower().StartsWith("sql自定义异常:"))
        //    {
        //        AlertMsg(msg.ToLower().Replace("sql自定义异常:", ""));
        //    }
        //    else
        //    {
        //        AlertMsg("插入失败，请检查数据是否异常");
        //    }
        //}
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        list = Session[str] as List<CompanyDetailEntity>;
        if (list == null)
        {
            list = new List<CompanyDetailEntity>();
        }
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];
        CompanyDetailEntity entity = new CompanyDetailEntity();
       // entity.Amount = txtAmount.Text;
        entity.CompanyId = company;
        entity.DetailTypesId = dropCurrency.SelectedValue;
        entity.Date = txtDate.Text.Trim() + "0101";
        //entity.Date = orderid + "0101";
        entity.OrderId = orderid;

        entity.tempId = list.Count + 1;

        list.Add(entity);
        Bind();

        txtAmount.Focus();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (DetailTypesList == null)
            {
                Bind();
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string str = e.Row.Cells[1].Text;

                e.Row.Cells[1].Text = DetailTypesList.First(fun => fun.TypeId == str).TypeName;
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        list = Session[str] as List<CompanyDetailEntity>;
        if (list == null)
        {
            list = new List<CompanyDetailEntity>();
        }

        list.Remove(list.First(fun => fun.tempId.Equals(e.Keys[0])));

        Bind();
    }
}